/**
 * Created with Vim7.3 Mac
 * @fileOverview : 
 * @author : Lyle lylechen2014@gmail.com
 * @since : 2016-08-31 17:37:19
 * @filename : src/base/share.js
 * @version : 
 * @description : horizontal sharing
 */
define(function(require, exports, module) {

    var share = {};

    share.regStorageListener = [];

    share.ajaxControl = function(obj) {
        var callback = obj.success;
        // var error = obj.error;
        var success = function(data,status,xhr) {console.log(data)
            if (data.errcode == 100) {
                //share.loginOut();
                window.location.href = '#signin/whole';
            } else if (data.errcode == 404) {
                //alert(data.errmsg);
                console.log(data.errcode);
                //share.pageLoad(false,$('body'));
                //share.browserClear();
                return;
            } else {
                callback(data,status,xhr);
            }
        };

        obj.async = (navigator.userAgent.indexOf('Firefox') < 0 && obj.async) ? false : true;
        obj.success = success;
        obj.timeout = 120000;
        obj.error = function(xhr, errorType, error) {
            // console.log(xhr,errorType,error);
            // share.pageLoad(false,$('body'));
            share.limitTime();
            if (error) {error(xhr,errorType,error);}
        };
        obj.xhrFields = {withCredentials: true};
        return obj;
    };

    share.limitTime = function() {
        var currentTime = new Date().getTime();
        var prevTime = util.getCookie('alert_time') || 0;
        if (currentTime - prevTime > 20000) {
            share.error('alert', 'Oops! There was a problem connecting to the network. Please try again!');
            util.setCookie('alert_time', currentTime, {path: '/',domain:util.rootDomain(domain)});
        }
    };

    //type console.log/alert
    share.error = function(type,msg,obj) {
         if (type == 'console') {
            console.log(msg);
         } else {
            alert(msg);
         }
    };

    share.accessPermission = function() {
        var bool = false,
            nmmid = util.getCookie('client_key');
        if (nmmid) {
            bool = true;
        }
        return bool;
    };

    share.permissionHandle = function() {
        var bool = this.accessPermission(),
            userInfo = this.userInfo(),
            agree = Util.getStorage("mspc_agree");

        if (bool) {
            if (parseInt(agree)!==1) {
                bool = false;
                window.location.href = '#'; 
            }
            if (!userInfo) {
                bool = false;
                share.resetLogin();
            }
        } else {
            bool = false;
            share.removeLocalStorage('mspc_userInfo');
            share.resetLogin();
        }
        return bool;
    };

    share.removeLocalStorage = function(key) {
        util.removeLocalStorage(key);
    };

    share.resetLogin = function() {
        
    };

    share.gotoUrl = function() {
        var cur_url = Util.getLocalStorage('mspc_url');
        if (cur_url) {

        } else {
            return '#';
        }
    };

    share.Login = function() {

    };

    share.LoginSuccess = function() {

    };

    share.userInfo = function() {
        return JSON.parse(Util.getLocalStorage('mspc_userInfo'));
    };

    share.repalceTpl = function(str,arr) {
        //str = 'aaa$0bbb$1ccc';
        var patten = new RegExp("\\$\\d{1}","g"),
            newStr = str.replace(patten,function(value){
               var index = parseInt(value.replace('$',''));
               return arr[index];
            });
        return newStr;
    };

    share.gotoModule = function(url,type) {   
        window.location.href = url;
    }

    module.exports = share;
});

